﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Core.Models;

namespace Core.Controllers
{
    public class UrlController : Controller
    {
        //
        // GET: /Url/

        public ActionResult Process(string Url)
        {
            Core.Tools.DebugInfo("Process Controlling Processing URL: " + Url);
            CoreEntities core = new CoreEntities();
            string pagename = "~/" + Url;
            Guid BUId = (Guid)Session["BusinessUnitId"];
            Guid DomainId = (Guid)Session["DomainId"];
            List<Page> list = core.Pages.Where(m => (m.PageName == pagename && m.IsDroppedYN == "N" && m.BusinessUnitId == BUId)).ToList();
            if (list.Count > 0)
            {
                if (UserPermissions.HasPermission(list[0].Permission.Name))
                {
                    string url = pagename.Substring(1);
                    if (core.MainMenus.Where(m => (m.URL == url && m.IsDroppedYN == "N" && m.DomainId == DomainId)).Count() > 0) Session["URL"] = pagename.Substring(1);
                    //if(list[0].OnMainMenuYN == "Y") Session["URL"] = pagename.Substring(1);
                    TempData["PageId"] = list[0].PageId;
                    return View("Page", list[0]);
                }
            }
            return View();
        }

        [ChildActionOnly]
        public ActionResult Container(Guid id)
        {
            CoreEntities core = new CoreEntities();
            List<Models.Container> list;
            Models.Container c = core.Containers.First(m => m.ContainerId == id);
            switch (c.ContainerType)
            {
                case "V":
                    list = core.Containers.Where(m => (m.ParentContainerId == id && m.IsDroppedYN == "N")).ToList();
                    return PartialView("VStackPanel",list);
                case "H":
                    list = core.Containers.Where(m => (m.ParentContainerId == id && m.IsDroppedYN == "N")).ToList();
                    return PartialView("HStackPanel",list);
                case "C":
                    Models.Plugin p = core.Plugins.First(m => m.PluginId == c.PluginId);
                    Guid PageId = (Guid)TempData["PageId"];
                    List<Models.TemplateOveride> olist = core.TemplateOverides.Where(m => (m.PageId == PageId && m.TemplateContentId == c.ContentId && m.IsDroppedYN == "N")).ToList();
                    ViewBag.ContentId = olist.Count > 0 ? olist[0].OverideContentId : c.ContentId;
                    ViewBag.Plugin = p;
                    return PartialView("Content");
            }
            return PartialView();
        }
    }
}
